package com.lanren.common.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.DrawableRes;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanren.common.R;

/**
 * @ClassName TitleLayoutView
 * @Description TODO 自定义标题栏
 * @Author chongheng.wang
 * @Date 2019/9/6 15:15
 * @Version 1.0
 */
public class TitleBarView extends LinearLayout {

    ImageView back;
    TextView titleTxt;
    ImageView more;
    TextView leftTxt;
    TextView rightTxt;
    TextView line;

    public TitleBarView(Context context) {
        super(context);
        initView(context);
    }

    public TitleBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        initAttr(context, attrs);
    }

    public TitleBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initAttr(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public TitleBarView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
        initAttr(context, attrs);
    }

    public void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.widget_title_layout_view, this, true);
        back = view.findViewById(R.id.back);
        titleTxt = view.findViewById(R.id.title_txt);
        more = view.findViewById(R.id.more);
        leftTxt = view.findViewById(R.id.left_txt);
        rightTxt = view.findViewById(R.id.right_txt);
        line = view.findViewById(R.id.line);
    }

    private void initAttr(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBarView);

        setLineShowOrHidd(typedArray.getBoolean(R.styleable.TitleBarView_title_view_line_show, false));

        line.setBackgroundColor(typedArray.getColor(R.styleable.TitleBarView_title_view_line, Color.parseColor("#00000000")));

        String title = typedArray.getString(R.styleable.TitleBarView_title_text);
        if (!TextUtils.isEmpty(title)) {
            titleTxt.setText(title);
        }
        titleTxt.setTextColor(typedArray.getColor(R.styleable.TitleBarView_title_text_color, Color.parseColor("#000000")));
        titleTxt.setTextSize(typedArray.getFloat(R.styleable.TitleBarView_title_text_size, 18));
        setTitleShowOrHidd(typedArray.getBoolean(R.styleable.TitleBarView_title_text_show, false));

        String left = typedArray.getString(R.styleable.TitleBarView_title_left_text);
        if (!TextUtils.isEmpty(left)) {
            leftTxt.setText(left);
        }
        leftTxt.setTextColor(typedArray.getColor(R.styleable.TitleBarView_title_left_text_color, Color.parseColor("#000000")));
        leftTxt.setTextSize(typedArray.getFloat(R.styleable.TitleBarView_title_left_text_size, 18));

        setLeftShowOrHidd(typedArray.getBoolean(R.styleable.TitleBarView_title_left_text_show, false));

        String right = typedArray.getString(R.styleable.TitleBarView_title_right_text);
        if (!TextUtils.isEmpty(right)) {
            rightTxt.setText(right);
        }
        rightTxt.setTextColor(typedArray.getColor(R.styleable.TitleBarView_title_right_text_color, Color.parseColor("#000000")));
        rightTxt.setTextSize(typedArray.getFloat(R.styleable.TitleBarView_title_right_text_size, 18));

        setRightShowOrHidd(typedArray.getBoolean(R.styleable.TitleBarView_title_right_text_show, false));

        Drawable backImg = typedArray.getDrawable(R.styleable.TitleBarView_title_back_img);
        if (backImg != null) {
            back.setImageDrawable(backImg);
        }
        setBackShowOrHidd(typedArray.getBoolean(R.styleable.TitleBarView_title_back_show, false));

        Drawable morImg = typedArray.getDrawable(R.styleable.TitleBarView_title_more_img);
        if (morImg != null) {
            more.setImageDrawable(morImg);
        }
        setMoreShowOrHidd(typedArray.getBoolean(R.styleable.TitleBarView_title_more_show, false));

        typedArray.recycle();
    }

    public void setBackListener(OnClickListener onClickListener) {
        back.setOnClickListener(onClickListener);
    }

    public void setLeftTxtListener(OnClickListener onClickListener) {
        leftTxt.setOnClickListener(onClickListener);
    }

    public void setTitleListener(OnClickListener onClickListener) {
        titleTxt.setOnClickListener(onClickListener);
    }

    public void setMoreListener(OnClickListener onClickListener) {
        more.setOnClickListener(onClickListener);
    }

    public void setRightTxtListener(OnClickListener onClickListener) {
        rightTxt.setOnClickListener(onClickListener);
    }

    public void setTitleTxt(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            titleTxt.setText(msg);
        }
    }

    public void setTitleTxtColor(String color) {
        if (!TextUtils.isEmpty(color)) {
            titleTxt.setTextColor(Color.parseColor(color));
        }
    }

    public void setTitleTxtSize(float size) {
        if (size != 0.0) {
            titleTxt.setTextSize(size);
        }
    }

    public void setLeftTxt(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            leftTxt.setText(msg);
        }
    }

    public void setRightTxt(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            rightTxt.setText(msg);
        }
    }

    public void setLeftTxtColor(String color) {
        if (!TextUtils.isEmpty(color)) {
            leftTxt.setTextColor(Color.parseColor(color));
        }
    }

    public void setRightTxtColor(String color) {
        if (!TextUtils.isEmpty(color)) {
            rightTxt.setTextColor(Color.parseColor(color));
        }
    }

    public void setLeftTxtSize(float size) {
        if (size != 0.0) {
            leftTxt.setTextSize(size);
        }
    }

    public void setRightTxtSize(float size) {
        if (size != 0.0) {
            rightTxt.setTextSize(size);
        }
    }

    public void setBackImg(Drawable drawable) {
        back.setImageDrawable(drawable);
    }

    public void setBackImg(@DrawableRes int img) {
        back.setImageResource(img);
    }

    public void setMoreImg(Drawable drawable) {
        more.setImageDrawable(drawable);
    }

    public void setMoreImg(@DrawableRes int img) {
        more.setImageResource(img);
    }


    public void setLineShowOrHidd(boolean titleShow) {
        if (titleShow) {
            line.setVisibility(View.VISIBLE);
        } else {
            line.setVisibility(View.GONE);
        }
    }

    public void setTitleShowOrHidd(boolean titleShow) {
        if (titleShow) {
            titleTxt.setVisibility(View.VISIBLE);
        } else {
            titleTxt.setVisibility(View.GONE);
        }
    }

    public void setBackShowOrHidd(boolean backShow) {
        if (backShow) {
            back.setVisibility(View.VISIBLE);
        } else {
            back.setVisibility(View.GONE);
        }
    }

    public void setMoreShowOrHidd(boolean moreShow) {
        if (moreShow) {
            more.setVisibility(View.VISIBLE);
        } else {
            more.setVisibility(View.GONE);
        }
    }

    public void setLeftShowOrHidd(boolean leftShow) {
        if (leftShow) {
            leftTxt.setVisibility(View.VISIBLE);
        } else {
            leftTxt.setVisibility(View.GONE);
        }
    }

    public void setRightShowOrHidd(boolean rightShow) {
        if (rightShow) {
            rightTxt.setVisibility(View.VISIBLE);
        } else {
            rightTxt.setVisibility(View.GONE);
        }
    }

}
